/**
 * 
 */
package au.edu.swin.videoshop.publicapp.ui.sections;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.ejb.EJB;
import javax.faces.component.UIData;
import javax.faces.component.UIViewRoot;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;

import au.edu.swin.videoshop.bean.Reservation;
import au.edu.swin.videoshop.bean.Store;
import au.edu.swin.videoshop.bean.Title;
import au.edu.swin.videoshop.bean.TitleMedium;
import au.edu.swin.videoshop.service.ReservationService;

public class ReservationSection implements Serializable {
	
	private Title mSelectedTitle;
	private TitleMedium mSelectedMedium;
	private Long mSelectedStoreId;
	
	private Integer mSelectedExpiryDateDay;
	private Integer mSelectedExpiryDateMonth;
	private Integer mSelectedExpiryDateYear;
	
	private List mAvailableStores;

	@EJB
	private ReservationService mReservationService;
	
	public ReservationSection() {
		Calendar cal = Calendar.getInstance();
		
		cal.add(Calendar.MONTH, 1);
		
		mSelectedExpiryDateDay = new Integer(cal.get(Calendar.DAY_OF_MONTH));
		mSelectedExpiryDateMonth = new Integer(cal.get(Calendar.MONTH));
		mSelectedExpiryDateYear = new Integer(cal.get(Calendar.YEAR));
	}

	/**
	 * @return the selectedTitle
	 */
	public Title getSelectedTitle() {
		return mSelectedTitle;
	}
	
	/**
	 * @return the selectedMedium
	 */
	public TitleMedium getSelectedMedium() {
		return mSelectedMedium;
	}

	/**
	 * @return the availableStores
	 */
	public List getAvailableStores() {
		return mAvailableStores;
	}

	/**
	 * @return the selectedStoreId
	 */
	public Long getSelectedStoreId() {
		return mSelectedStoreId;
	}

	/**
	 * @param selectedStoreId the selectedStoreId to set
	 */
	public void setSelectedStoreId(Long selectedStoreId) {
		mSelectedStoreId = selectedStoreId;
	}

	/**
	 * @return the selectedExpiryDateDay
	 */
	public Integer getSelectedExpiryDateDay() {
		return mSelectedExpiryDateDay;
	}

	/**
	 * @param selectedExpiryDateDay the selectedExpiryDateDay to set
	 */
	public void setSelectedExpiryDateDay(Integer selectedExpiryDateDay) {
		mSelectedExpiryDateDay = selectedExpiryDateDay;
	}

	/**
	 * @return the selectedExpiryDateMonth
	 */
	public Integer getSelectedExpiryDateMonth() {
		return mSelectedExpiryDateMonth;
	}

	/**
	 * @param selectedExpiryDateMonth the selectedExpiryDateMonth to set
	 */
	public void setSelectedExpiryDateMonth(Integer selectedExpiryDateMonth) {
		mSelectedExpiryDateMonth = selectedExpiryDateMonth;
	}

	/**
	 * @return the selectedExpiryDateYear
	 */
	public Integer getSelectedExpiryDateYear() {
		return mSelectedExpiryDateYear;
	}

	/**
	 * @param selectedExpiryDateYear the selectedExpiryDateYear to set
	 */
	public void setSelectedExpiryDateYear(Integer selectedExpiryDateYear) {
		mSelectedExpiryDateYear = selectedExpiryDateYear;
	}

	/**
	 * @param selectedMedium the selectedMedium to set
	 */
	public void setSelectedMedium(TitleMedium selectedMedium) {
		mSelectedMedium = selectedMedium;
		mSelectedTitle = selectedMedium.getTitle();
		generateAvailableStores();
	}

	private void generateAvailableStores() {
		if (mSelectedMedium != null) {
			List stores = mSelectedMedium.getAvailableStores();
			
			mAvailableStores = new ArrayList();
			mAvailableStores.add(new SelectItem(null, "Select a store"));
			for (Iterator i = stores.iterator(); i.hasNext();) {
				Store store = (Store)i.next();
				
				mAvailableStores.add(new SelectItem(store.getStoreId(), store.getStoreName()));
			}
		}
	}

	public String finishReservation() {
		FacesContext facesContext = FacesContext.getCurrentInstance();
		UserSection userSection = (UserSection)facesContext.getApplication().evaluateExpressionGet(facesContext, "#{userSection}", UserSection.class);
		
		if (userSection.getCustomer() != null &&
			mSelectedMedium != null && mSelectedStoreId != null && 
			mSelectedExpiryDateDay != null && mSelectedExpiryDateMonth != null && mSelectedExpiryDateYear != null) {
			
			Store selectedStore = null;
			List stores = mSelectedMedium.getAvailableStores();
			for (Iterator i = stores.iterator(); i.hasNext();) {
				Store store = (Store)i.next();
				
				if (store.getStoreId().equals(mSelectedStoreId)) {
					selectedStore = store;
					break;
				}
			}
			
			if (selectedStore != null) {
				Calendar cal = Calendar.getInstance();
				cal.clear();
				cal.set(mSelectedExpiryDateYear.intValue(), mSelectedExpiryDateMonth.intValue(), mSelectedExpiryDateDay.intValue());
				
				Reservation reservation = new Reservation();
				reservation.setCustomer(userSection.getCustomer());
				reservation.setMedium(mSelectedMedium);
				reservation.setStore(selectedStore);
				reservation.setReservationDate(new Date());
				reservation.setExpiryDate(cal.getTime());
				
				mReservationService.addReservation(reservation);
				
				userSection.refreshReservations();
				
				return "myaccount";
			}
		}
		
		return null;
		
	}
	
	
	public String cancelReservation() {
		FacesContext facesContext = FacesContext.getCurrentInstance();
		UserSection userSection = (UserSection)facesContext.getApplication().evaluateExpressionGet(facesContext, "#{userSection}", UserSection.class);
		
		if (userSection.getCustomer() != null) {
			UIViewRoot root = facesContext.getViewRoot();
			UIData table = (UIData)root.findComponent("reservationForm").findComponent("reservationTable");
			
			Reservation reservation = (Reservation) table.getRowData();
			
			mReservationService.cancelReservation(userSection.getCustomer(), reservation.getReservationId());
			
			userSection.refreshReservations();
		}
		
		return null;
	}
}
